package com.qbw.app.module.common.fragment;

import android.app.Activity;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.view.View;

import com.lib.util.DateTimeUtil;
import com.lib.util.ResUtil;
import com.qbw.app.R;
import com.qbw.app.databinding.CommonFragmentDateSelectBinding;
import com.qbw.core.base.BaseBottomDialogFragment;
import com.weigan.loopview.OnItemSelectedListener;

import java.util.ArrayList;
import java.util.List;

/**
 * @author qinbaowei
 * @date 2018/2/28
 * @email qbaowei@qq.com
 */

public class DateSelectDialogFragment extends BaseBottomDialogFragment<CommonFragmentDateSelectBinding> {

    public static DateSelectDialogFragment createInstance() {
        DateSelectDialogFragment ins = new DateSelectDialogFragment();
        return ins;
    }

    @Override
    protected int onGetLayoutId() {
        return R.layout.common_fragment_date_select;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        onInit();
    }

    protected void onInit() {
        final int[] ymd = DateTimeUtil.getCurrentYMD();
        final List<String> years = new ArrayList<>();
        for (int i = ymd[0] - 3; i <= ymd[0]; i++) {
            years.add(i + "");
        }
        mBinding.lvYear.setNotLoop();
        mBinding.lvYear.setItems(years);
        mBinding.lvYear.setInitPosition(years.size() - 1);
        List<String> months = new ArrayList<>();
        int[] monthStrRess = new int[]{
                R.string.month_one,
                R.string.month_two,
                R.string.month_three,
                R.string.month_four,
                R.string.month_five,
                R.string.month_six,
                R.string.month_seven,
                R.string.month_eight,
                R.string.month_nine,
                R.string.month_ten,
                R.string.month_eleven,
                R.string.month_twelve
        };
        for (Integer m : monthStrRess) {
            months.add(ResUtil.getString(m));
        }
        mBinding.lvMonth.setItems(months);
        mBinding.lvMonth.setInitPosition(ymd[1] - 1);
        mBinding.lvMonth.setListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                int year = Integer.parseInt(years.get(mBinding.lvYear.getSelectedItem()));
                int month = mBinding.lvMonth.getSelectedItem() + 1;
                if (year == ymd[0] && month > ymd[1]) {
                    mBinding.lvMonth.setCurrentPosition(ymd[1] - 1);
                }
            }
        });

        mBinding.txtCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        mBinding.txtOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Listener listener = getListener();
                if (listener != null) {
                    int year = Integer.parseInt(years.get(mBinding.lvYear.getSelectedItem()));
                    int month = mBinding.lvMonth.getSelectedItem() + 1;
                    listener.onStrokeDateSelect(year, month);
                }
                dismiss();
            }
        });
    }

    private Listener getListener() {
        Fragment pf = getParentFragment();
        if (pf instanceof Listener) {
            return (Listener) pf;
        }
        Activity act = getActivity();
        if (act instanceof Listener) {
            return (Listener) act;
        }
        return null;
    }

    public interface Listener {
        void onStrokeDateSelect(int year, int month);
    }
}
